package me.zhengjie.suli.dashboard.service.impl;

import lombok.RequiredArgsConstructor;
import me.zhengjie.suli.dashboard.repository.DashboardRepository;
import me.zhengjie.suli.dashboard.service.DashboardService;
import me.zhengjie.suli.inventory.domain.Inventory;
import me.zhengjie.suli.inventory.repository.InventoryRepository;
import me.zhengjie.suli.tasks.domain.TaskDetail;
import me.zhengjie.suli.tasks.domain.Tasks;
import me.zhengjie.suli.tasks.repository.TaskDetailRepository;
import me.zhengjie.suli.tasks.repository.TasksRepository;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class DashboardServiceImpl implements DashboardService {
    final DashboardRepository dashboardRepository;

    final InventoryRepository inventoryRepository;
    private final TasksRepository tasksRepository;
    private final TaskDetailRepository taskDetailRepository;

    @Override
    public Map<String, List<Integer>> getDataByType(String type) {
        Map<String, List<Integer>> dataList = dashboardRepository.getDataByType(type);
        return dataList;
    }

    @Override
    public Map<String, Integer> getPieChartData() {
        Map<String, Integer> resultMap = dashboardRepository.getPieChartData();
        return resultMap;
    }

    @Override
    public Map<String, List<Integer>> getBarchartData() {
        Map<String, List<Integer>> resultMap = dashboardRepository.getBarchartData();
        return resultMap;
    }

    @Override
    public Map<String, List<Integer>> getBarchartDataOutHouse() {
        Map<String, List<Integer>> resultMap = dashboardRepository.getBarchartDataOutHouse();
        return resultMap;
    }

    @Override
    public Map<String, Integer> getBarchartLocationData() {
        Map<String, Integer> resultMap = dashboardRepository.getBarchartLocationData();
        return resultMap;
    }

    @Override
    public Map<String, Integer> getTodayNumber() {
        Map<String, Integer> resultMap = dashboardRepository.getTodayNumber();
        return resultMap;
    }

    @Override
    public Map<String, Integer[]> getTimeNumber() {
        Map<String, Integer[]> resultMap = dashboardRepository.getTimeNumber();
        return resultMap;
    }

    @Override
    public Map<String, int[]> getOneWeekToAndOutData() {
        return dashboardRepository.getOneWeekToAndOutData();
    }

    @Override
    public List<Inventory> getInventory() {
//        List<Inventory> inventoryList = inventoryRepository.getAllInventoryWithNoTrayCode();
//        return inventoryList;
        return null;
    }

    @Override
    public List<Map<String, Object>> getInventory2(int pageNum) {
        List<Map<String, String>> inventoryList = inventoryRepository.getInventoriesByMaterialCodeGroupWithNoTrayCode(pageNum);
        List<Map<String, Object>> list1 = new ArrayList<>();
        for (Map<String, String> map : inventoryList) {
            Map<String, Object> modifyMap = new HashMap<>(map);
            String materialCode = map.get("material_code");
            modifyMap.put("secInv", 0);
            list1.add(modifyMap);
        }
        return list1;
    }

    @Override
    public List<Map<String, String>> getTasks() {
        List<Tasks> tasksList = tasksRepository.getTop20TasksWithStatus1();
        List<Map<String, String>> returnTaskList = new ArrayList<>();
        for (Tasks tasks : tasksList) {
            List<TaskDetail> taskDetailList = taskDetailRepository.getTaskDetailsByTaskId(tasks.getId());
            for (TaskDetail taskDetail : taskDetailList) {
                Map<String, String> map2 = new HashMap<>();
                map2.put("lot_code", taskDetail.getLotNo());
                map2.put("qty", String.valueOf(taskDetail.getPkQty()));
                switch (String.valueOf(tasks.getTaskType())) {
                    case "1":   // 入库
                        map2.put("task_type", "入库");
                        break;
                    case "2":   // 出库
                        map2.put("task_type", "出库");
                        break;
                    case "3":   // 移库
                        map2.put("task_type", "移库");
                        break;
                    case "5":   // 盘点出库
                        map2.put("task_type", "盘点出库");
                        break;
                    case "8":   // 回库
                        map2.put("task_type", "回库");
                        break;
                    case "9":   // 托盘入库
                        map2.put("task_type", "托盘入库");
                        break;
                    case "10":   // 托盘出库
                        map2.put("task_type", "托盘出库");
                        break;
                    case "11":   // 空载具回库
                        map2.put("task_type", "空载具回库");
                        break;
                    default:
                        map2.put("task_type", "未知");
                        break;
                }
                map2.put("material_code", tasks.getElName());
                map2.put("task_status", "执行中");
                returnTaskList.add(map2);
            }
        }
        return returnTaskList;
    }
}
